package com.doge.walknovel.business.home.profile

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.doge.walknovel.api.*
import com.doge.walknovel.api.model.PushEnableInfo
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.utils.*

class PushViewModel : ViewModel() {
    companion object {
        const val UPDATE_ALL = 1
        const val UPDATE_CHAPTER = 2
        const val UPDATE_ACTIVITY = 3
        private const val folder = "push"
        private const val key = "data"
    }

    val loading = MutableLiveData<LoadStatus>()
    val setLoading = MutableLiveData<Pair<Int, LoadStatus>>()
    val enableInfo = MutableLiveData<PushEnableInfo>()

    fun requestEnableInfo() {
        require(isMainThread())

        runOnIO {
            if (enableInfo.value == null) {
                val disk: String? = spReadString(key, null, folder)
                val cache: PushEnableInfo? = fromJson(disk, PushEnableInfo::class.java)
                runOnUI {
                    if (cache == null) loading.value = LoadStatus.LOADING
                    cache?.let { enableInfo.value = it }
                }
            }
            api.pushEnableList()
                .map(ApiResultMapTransformer())
                .subscribeOnIO()
                .observeOnMain()
                .subscribe(object : ObserverAdapter<PushEnableInfo>() {
                    override fun onNext(t: PushEnableInfo) {
                        enableInfo.value = t.copy(appUpdate = false)
                        loading.value = LoadStatus.SUCCESS
                        runOnUI { spWrite(key, toJson(t), folder) }
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        loading.value = LoadStatus.FAILURE
                    }
                })
        }
    }

    fun requestSetEnable(chapterUpdatePush: Int?, activityPush: Int?) {
        require(isMainThread())
        val type = when {
            chapterUpdatePush != null && activityPush != null -> UPDATE_ALL
            chapterUpdatePush != null -> UPDATE_CHAPTER
            activityPush != null -> UPDATE_ACTIVITY
            else -> return
        }
        setLoading.value = Pair(type, LoadStatus.LOADING)
        api
            .pushManagement(chapterUpdatePush, activityPush)
            .compose(ApiResultComposeTransformer())
            .flatMap { api.pushEnableList() }
            .map(ApiResultMapTransformer())
            .subscribeOnIO()
            .observeOnMain()
            .subscribe(object : ObserverAdapter<PushEnableInfo>() {
                override fun onNext(t: PushEnableInfo) {
                    enableInfo.value = t.copy(appUpdate = true)
                    setLoading.value = Pair(type, LoadStatus.SUCCESS)
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    setLoading.value = Pair(type, LoadStatus.FAILURE)
                }
            })
    }
}